package sh.wnxy.wn38.controller;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wnxy.wn38.entity.Img;
import sh.wnxy.wn38.service.IImgService;
import sh.wnxy.wn38.utils.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-14
 */
@Slf4j
@ApiModel("图片管理")
@CrossOrigin
@RestController
@RequestMapping("/img")
public class ImgController {
    private IImgService iImgService;

    @Autowired
    public void setiImgService(IImgService iImgService) {
        this.iImgService = iImgService;
    }

    @ApiOperation("删除图片")
    @GetMapping("/deleteImg/{id}")
    public Result deleteImg(@ApiParam(value = "图片编号", required = true) @PathVariable("id") Integer imgId) {
        boolean result = iImgService.removeById(imgId);
        return result ? Result.ok() : Result.error();
    }

    @ApiOperation("添加图片")
    @PostMapping("/addImg")
    public Result addImg(@ApiParam(value = "图片信息") @RequestBody Img img) {
        boolean save = iImgService.save(img);
        return save ? Result.ok() : Result.error();
    }

    @ApiOperation("查询图片")
    @GetMapping("/findImg/{id}")
    public Result findImgByImgId(@PathVariable("id") Integer imgId) {
        log.warn(imgId+"");
        Img byId = iImgService.getById(imgId);
        return byId != null ? Result.ok().data("mainImg", byId) : Result.error();
    }


}

